Next: 4.6.18 PIC16 Port - Up: 4.6 The PIC16 port Previous: 4.6.16 Generic Pointers Contents Index
Subsections

- 4.6.17.1 Standard I/O Streams
- 4.6.17.2 Printing functions
- 4.6.17.3 Signals

## 4.6.17 PIC16 C Libraries

### 4.6.17.1 Standard I/O Streams

In the stdio.h the type FILE is defined as:

  typedef char * FILE;

This type is the stream type implemented I/O in the PIC18F devices. Also the standard input and output streams are declared in stdio.h:

  extern FILE * stdin;

  extern FILE * stdout;

The FILE type is actually a generic pointer which defines one more type of generic pointers, the stream pointer. This new type has the format:

pointer type | <7:6> | <5> | <4> | <3:0> | rest of the pointer | descrption
stream | 00 | 1 | 0 | nnnn | `uuuuuuuu uuuuuuuu` | upper byte high nubble is 0x2n, the rest are zeroes

Currently implemented there are 3 types of streams defined:

stream type | value | module | description
STREAM_USART | `0x200000UL` | USART | Writes/Reads characters via the USART peripheral
STREAM_MSSP | `0x210000UL` | MSSP | Writes/Reads characters via the MSSP peripheral
STREAM_USER | `0x2f0000UL` | (none) | Writes/Reads characters via used defined functions

The stream identifiers are declared as macros in the stdio.h header.

In the libc library there exist the functions that are used to write to each of the above streams. These are

__stream_usart_putchar
  writes a character at the USART stream
__stream_mssp_putchar
  writes a character at the MSSP stream
putchar
  dummy function. This writes a character to a user specified manner.

In order to increase performance putchar is declared in stdio.h as having its parameter in WREG (it has the wparam keyword). In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function in a user-friendly way. arg is the name of the variable that holds the character to print. An example follows:

  #include <pic18fregs.h>
  #include <stdio.h>
  PUTCHAR( c )

  {

  PORTA = c; /* dump character c to PORTA */

  }
  void main(void)

  {

  stdout = STREAM_USER; /* this is not necessary, since stdout points

  * by default to STREAM_USER */

  printf (''This is a printf test\n'');

  }

### 4.6.17.2 Printing functions

PIC16 contains an implementation of the printf-family of functions. There exist the following functions:

  extern unsigned int sprintf(char *buf, char *fmt, ...);

  extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);

  extern unsigned int printf(char *fmt, ...);

  extern unsigned int vprintf(char *fmt, va_lista ap);

  extern unsigned int fprintf(FILE *fp, char *fmt, ...);

  extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);

For sprintf and vsprintf buf should normally be a data pointer where the resulting string will be placed. No range checking is done so the user should allocate the necessary buffer. For fprintf and vfprintf fp should be a stream pointer (i.e. stdout, STREAM_MSSP, etc...).

### 4.6.17.3 Signals

The PIC18F family of microcontrollers supports a number of interrupt sources. A list of these interrupts is shown in the following table:

signal name | description | signal name | description
SIG_RB | PORTB change interrupt | SIG_EE | EEPROM/FLASH write complete interrupt
SIG_INT0 | INT0 external interrupt | SIG_BCOL | Bus collision interrupt
SIG_INT1 | INT1 external interrupt | SIG_LVD | Low voltage detect interrupt
SIG_INT2 | INT2 external interrupt | SIG_PSP | Parallel slave port interrupt
SIG_CCP1 | CCP1 module interrupt | SIG_AD | AD convertion complete interrupt
SIG_CCP2 | CCP2 module interrupt | SIG_RC | USART receive interrupt
SIG_TMR0 | TMR0 overflow interrupt | SIG_TX | USART transmit interrupt
SIG_TMR1 | TMR1 overflow interrupt | SIG_MSSP | SSP receive/transmit interrupt
SIG_TMR2 | TMR2 matches PR2 interrupt |  |
SIG_TMR3 | TMR3 overflow interrupt |  |

The prototypes for these names are defined in the header file signal.h.

In order to simplify signal handling, a number of macros is provided:

- [DEF_INTHIGH(name)] begin the definition of the interrupt dispatch table for high priority interrupts. name is the function name to use.
- [DEF_INTLOW(name)] begin the definition of the interrupt dispatch table fo low priority interrupt. name is the function name to use.
- [DEF_HANDLER(sig,handler)] define a handler for signal sig.
- [END_DEF] end the declaration of the dispatch table.

Additionally there are two more macros to simplify the declaration of the signal handler:

- [SIGHANDLER(handler)] this declares the function prototype for the handler function.
- [SIGHANDLERNAKED(handler)] same as SIGHANDLER() but declares a naked function.

An example of using the macros above is shown below:

  #include <pic18fregs.h>

  #include <signal.h>
  DEF_INTHIGH(high_int)

  DEF_HANDLER(SIG_TMR0, _tmr0_handler)

  DEF_HANDLER(SIG_BCOL, _bcol_handler)

  END_DEF
  SIGHANDLER(_tmr0_handler)

  {

  /* action to be taken when timer 0 overflows */

  }
  SIGHANDLERNAKED(_bcol_handler)

  {

  __asm

  /* action to be taken when bus collision occurs */

  retfie

  __endasm;

  }

NOTES: Special care should be taken when using the above scheme:

- do not place a colon (;) at the end of the DEF_* and END_DEF macros.
- when declaring SIGHANDLERNAKED handler never forget to use retfie for proper returning.

Next: 4.6.18 PIC16 Port - Up: 4.6 The PIC16 port Previous: 4.6.16 Generic Pointers Contents Index